//author:kakake
class tsgbdrugController {
    public data = {
        dbConn: TSLobsterAPI.Program.GetDbConn()
    }

    //获取药理分类
    gettree() {

        var strsql = "SELECT PharmID id,PharmName nodeName,parentId FROM DG_GB_Pharmacology";
        var list = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql);

        var _root = TSLobsterAPI.Utils.findlist(list, 'parentId', '-1');//根节点

        var tree = [];
        var lv = 1;
        _root.forEach(i => {
            var node = {};
            node["id"] = i['id'];
            node["name"] = i['nodeName'];
            node["title"] = i['nodeName'];
            node["parentid"] = i['parentId'];
            node["spread"] = true;
            node["href"] = "";
            node["selected"] = false;
            node["disabled"] = false;
            node["level"] = 1;
            node["children"] = this.bulidtree(list, i['id'], lv + 1);
            tree.push(node);
        });

        return tree;
    }
    //绑定树
    bulidtree(List, pid, lv) {

        var _childlist = TSLobsterAPI.Utils.findlist(List, 'parentId', pid);
        var treelist = [];
        _childlist.forEach(i => {
            var node = {};
            node["id"] = i['id'];
            node["name"] = i['nodeName'];
            node["title"] = i['nodeName'];
            node["parentid"] = i['parentId'];
            node["spread"] = true;
            node["href"] = "";
            node["selected"] = false;
            node["disabled"] = false;
            node["level"] = lv;
            node["children"] = this.bulidtree(List, i['id'], lv + 1);
            treelist.push(node);
        });

        return treelist;
    }

    //获取基药数据
    getDrugdata() {
        var cid = TSLobsterAPI.Program.RequestQuery("cid", "");
        var lv = TSLobsterAPI.Program.RequestQuery("lv", "");
        var exactQuery = TSLobsterAPI.Program.RequestQuery("exactQuery", "");
        var drugName = TSLobsterAPI.Program.RequestQuery("drugName", "");
        var dosagesName = TSLobsterAPI.Program.RequestQuery("dosagesName", "");

        var pageInfo = {
            key: "BasicDrugID",
            page: TSLobsterAPI.Program.RequestQuery('page', 0),
            perPage: TSLobsterAPI.Program.RequestQuery('limit', 10),
            totalRecord: 0,
            totalPage: 0
        }

        var strsql = "";
        strsql = "SELECT BasicDrugID,BasicDrugName,DrugName,Spec,b.DosageName Dosage, \
        (case IsRecipe when 0 then '否' when 1 then '是'  end) IsRecipe, \
        (case Class when 1 then '化学药品和生物制药' when 2 then '中成药' when 3 then '中草药' end) Class \
        FROM DG_GB_SpecDic a INNER JOIN DG_GB_DosageDic b on a.DosageID=b.DosageID and a.WorkID=b.WorkID where 1=1 ";

        if (parseInt(lv) == 1) {
            strsql = strsql + " and a.TypeID in (select PharmID from  DG_GB_Pharmacology  where DelFlag=0 and ParentID=@cid)";
        } else if (parseInt(lv) == 2) {
            strsql = strsql + " and a.TypeID = @cid";
        } else if (parseInt(lv) == 3) {
            strsql = strsql + " and a.CTypeID = @cid";
        }
        else if (parseInt(lv) == 4) {
            strsql = strsql + " and a.PharmID = @cid";
        }

        if (drugName != "") {
            if (exactQuery == "true") {
                strsql = strsql + " and DrugName = @drugName";
            } else {
                strsql = strsql + " and DrugName like @_drugName ";
            }
        }

        var dbparams = {
            cid: cid,
            drugName: drugName,
            _drugName: '%' + drugName + '%'
        }

        //获取分页Sql和条数
        var _list = TSLobsterAPI.DbHelper.DbGetJsonData(this.data.dbConn, strsql, dbparams, pageInfo);
        var List = [];
        _list.forEach(result => {
            var item = {};
            item["药品ID"] = result["BasicDrugID"];
            item["品种名称"] = result["BasicDrugName"];
            item["药品名称"] = result["DrugName"];
            item["规格"] = result["Spec"];
            item["剂型"] = result["Dosage"];
            item["类型"] = result["Class"];
            item["处方资质"] = result["IsRecipe"];
            List.push(item);
        })
        return { List: List, count: pageInfo.totalRecord };
    }
}